package unix

import (
	"context"
	"fmt"
	"time"
	"ultone/internal/log"
	"ultone/internal/opt"
)

type Handler struct {
	Ctx context.Context
}

type AvailableReq struct{}
type AvailableResp struct {
	OK       bool
	Now      time.Time
	Start    time.Time
	Duration string
}

func (*Handler) Available(_ *AvailableReq, out *AvailableResp) error {
	now := time.Now()
	out.OK, out.Now = opt.OK, now
	out.Start = opt.Start
	out.Duration = fmt.Sprint(now.Sub(opt.Start))
	return nil
}

type SettingReq struct {
	Debug bool
}
type Resp[T any] struct {
	Status uint32
	Msg    string
	Data   T
}

func (h *Handler) Setting(in *SettingReq, out *Resp[bool]) error {
	opt.Locker.Lock()
	defer opt.Locker.Unlock()

	if in.Debug {
		opt.Debug = true
		log.Info(h.Ctx, "set global debug[true]")
	} else {
		opt.Debug = false
		log.Info(h.Ctx, "set global debug[false]")
	}

	out.Status = 200
	out.Msg = "操作成功"

	return nil
}
